package com.yh.mybatis.reflection;

import com.yh.mybatis.reflection.factory.ObjectFactory;
import com.yh.mybatis.reflection.property.PropertyTokenizer;
import com.yh.mybatis.reflection.wrapper.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 元对象
 *
 * @author yh
 * @date 2023/09/27
 */
public class MetaObject {

    /**
     * 原始对象
     */
    private Object originalObject;

    /**
     * 对象包装器
     */
    private ObjectWrapper objectWrapper;

    /**
     * 对象工厂
     */
    private ObjectFactory objectFactory;

    /**
     * 对象包装器工厂
     */
    private ObjectWrapperFactory objectWrapperFactory;

    public MetaObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory) {
        this.originalObject = object;
        this.objectFactory = objectFactory;
        this.objectWrapperFactory = objectWrapperFactory;

        if (object instanceof ObjectWrapper) {
            this.objectWrapper = (ObjectWrapper) object;
        } else if (objectWrapperFactory.hasWrapperFor(object)) {
            this.objectWrapper = objectWrapperFactory.getWrapperFor(this, object);
        } else if (object instanceof Map) {
            this.objectWrapper = new MapWrapper(this, (Map) object);
        } else if (object instanceof Collection) {
            this.objectWrapper = new CollectionWrapper(this, (Collection)object);
        } else {
            this.objectWrapper = new BeanWrapper(this, object);
        }
    }

    public static MetaObject forObject(Object value, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory) {
        if (value == null) {
            return SystemMetaObject.NULL_META_OBJECT;
        } else {
            return new MetaObject(value, objectFactory, objectWrapperFactory);
        }
    }
    public ObjectFactory getObjectFactory() {
        return objectFactory;
    }

    public ObjectWrapperFactory getObjectWrapperFactory() {
        return objectWrapperFactory;
    }

    public Object getOriginalObject(){
        return originalObject;
    }

    /* -------------- 以下方法是委托给objectWrapper ---------------*/

    // 查找属性
    public String findProperty(String propName, boolean useCamelCaseMapping) {
        return objectWrapper.findProperty(propName,useCamelCaseMapping);
    }

    public String[] getGetterNames(){
        return objectWrapper.getGetterNames();
    }

    public String[] getSetterNames(){
        return objectWrapper.getSetterNames();
    }

    public Class<?> getGetterType(String  name){
        return objectWrapper.getGetterType(name);
    }

    // 获取 setter的类型
    public Class<?> getSetterType(String name){
        return objectWrapper.getSetterType(name);
    }

    // 是否有指定的getter
    public boolean hasGetter(String name){
        return objectWrapper.hasGetter(name);
    }

    // 是否有指定的setter
    public boolean hasSetter(String name){
        return objectWrapper.hasSetter(name);
    }

    public Object getValue(String name) {
        PropertyTokenizer prop = new PropertyTokenizer(name);
        if (prop.hasNext()){
            MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
            if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
                return null;
            } else {
                return metaValue.getValue(prop.getChildren());
            }
        } else {
            return objectWrapper.get(prop);
        }
    }

    public void setValue(String name, Object value){
        PropertyTokenizer prop = new PropertyTokenizer(name);
        if (prop.hasNext()){
            MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
            if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
                if (value == null && prop.getChildren() != null) {
                    return;
                } else {
                    metaValue = objectWrapper.instantiatePropertyValue(name,prop,objectFactory);
                }
            }
            metaValue.setValue(prop.getChildren(),value);
        } else {
            objectWrapper.set(prop,value);
        }
    }

    // 为属性生成元对象
    public MetaObject metaObjectForProperty(String indexedName) {
        Object value = getValue(indexedName);
        return MetaObject.forObject(value,objectFactory,objectWrapperFactory);
    }

    public ObjectWrapper getObjectWrapper( ){
        return objectWrapper;
    }

    public boolean isCollection(){
        return objectWrapper.isCollection();
    }

    public void add(Object element){
        objectWrapper.add(element);
    }

    public <E> void addAll(List<E> list){
        objectWrapper.addAll(list);
    }
}
